package ma.co.ba.BusinessApplications.business.clients.impl;

import java.util.ArrayList;
import java.util.List;

import ma.co.ba.BusinessApplications.business.clients.ContactBusiness;
import ma.co.ba.BusinessApplications.dao.commons.DAOServices;
import ma.co.ba.BusinessApplications.model.Contact;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("contactBusiness")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ContactBusinessImpl implements ContactBusiness {

	@Autowired
	DAOServices daoServices;

	public void saveContact(Contact contact) {
		daoServices.saveEntity(contact);
	}

	public Contact getContactById(int id) {

		return (Contact) daoServices.getEntityById2(Contact.class, id);
	}

	public List<Contact> getAllContacts() {

		return daoServices.getEntities(Contact.class);
	}

	public List<Contact> getActiveContacts() {
		List<Contact> allContacts = new ArrayList<Contact>();
		List<Contact> activeContacts = new ArrayList<Contact>();
		allContacts = (List<Contact>) daoServices
				.getEntities(Contact.class);
		if (allContacts != null)
		{
			for (Contact contact : allContacts) {
				if (contact.isActif()) {
					activeContacts.add(contact);
				}
			}
		}
		return activeContacts;
	}

	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void disableContact(int contactId) {
		Contact contact = (Contact) daoServices.getEntityById2(Contact.class,
				contactId);
		contact.setActif(false);
		daoServices.updateEntity(contact);

	}

}
